<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use common\services\ExhibitionCategoryServices;

$categoryService = new ExhibitionCategoryServices();
$category = $categoryService->getData(null);
/* @var $this yii\web\View */
/* @var $model common\models\Exhibitor */
/* @var $form yii\widgets\ActiveForm */
?>
<?=Html::jsFile('/static/common/js/ajaxfileupload.js')?>
<?=Html::cssFile('/static/common/bootstrap-datetimepicker/css/bootstrap-datetimepicker.min.css')?>
<?=Html::jsFile('/static/common/bootstrap-datetimepicker/js/bootstrap-datetimepicker.js')?>
<style>
.field-exhibitor-registered_capital .help-block {display: none;}
</style>
<div class="exhibitor-form">

    <?php $form = ActiveForm::begin(); ?>
    <table class="table">
        <tr>
            <td width="50%"><?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?></td>
            <td><?= $form->field($model, 'status')->dropDownList(\common\helpers\TStringHelper::getStatusText()) ?></td>
        </tr>
        <tr>
            <td><?= $form->field($model, 'telphone')->textInput(['maxlength' => true]) ?></td>
            <td><?= $form->field($model, 'postcode')->textInput(['type' => 'number']) ?></td>
        </tr>
        <tr>
            <td><?= $form->field($model, 'url')->textInput(['maxlength' => true]) ?></td>
            <td>
                <div class="col-xs-6 col-md-6">
                    <label class="control-label"><?=Yii::t('common', 'start_time')?></label>
                    <div class="form-group">
                        <div class="input-group">
                            <input type="text" class="form-control" id="start_time" name="Exhibitor[start_time]" value="<?=!empty($model->start_time)?date('Y-m-d H:i'):''?>" readonly placeholder="<?=Yii::t('common', 'click_select')?>">
                            <span class="input-group-addon"><i class="fa fa-remove icon-remove"></i></span>
                        </div>
                    </div>
                </div>
                <div class="col-xs-6 col-md-6">
                    <label class="control-label"><?=Yii::t('common', 'registered_capital')?></label>
                    <div class="input-group">
                        <?= $form->field($model, 'registered_capital')->textInput(['class' => 'form-control'])->label(false) ?>
                        <div class="input-group-addon">万元</div>
                    </div>

                </div>
            </td>
        </tr>
        <tr>
            <td>
                <div class="form-group">
                    <label class="control-label"><?=Yii::t('common', 'region')?></label>
                    <div class="form-group">
                        <div class="row">
                            <div class="col-xs-3 col-md-3 province">
                                <select name="Exhibitor[province_id]" class="form-control region_id" id="province" onchange="getRegion(this, 'city')">
                                    <option value=""><?=Yii::t('common', 'select...')?></option>
                                </select>
                            </div>
                            <div class="col-xs-3 col-md-3 city">
                                <select name="Exhibitor[city_id]" class="form-control region_id" id="city">
                                    <option value=""><?=Yii::t('common', 'select...')?></option>
                                </select>
                            </div>
                            <!--div class="col-xs-3 col-md-3 area" style="display: none;">
                                <select name="region[]" class="form-control region_id" id="area">
                                    <option value=""><?/*=Yii::t('common', 'select...')*/?></option>
                                </select>
                            </div>-->
                        </div>
                    </div>
                </div>
            </td>
            <td><?= $form->field($model, 'address')->textInput(['maxlength' => true]) ?></td>
        </tr>
        <tr>
            <td>
                <?= $form->field($model, 'logo')->textInput(['id' => 'logo_input']) ?>
                <input type="file" name="Filedata" class="file_upload_attach" id="file_upload_logo" data-id="logo">
            </td>
            <td>
                <?= $form->field($model, 'license')->textInput(['id' => 'license_input']) ?>
                <input type="file" name="Filedata" class="file_upload_attach" id="file_upload_license" data-id="license">
            </td>
        </tr>
        <tr>
            <td>
                <div class="form-group">
                    <label class="control-label"><?=Yii::t('common', 'category')?></label>
                    <select id="exhibition-category_id" class="form-control" name="Exhibitor[industry_id]">
                        <option value=""><?=Yii::t('common', 'select...')?></option>
                        <?php
                        if (!empty($category['parent'])) {
                            foreach ($category['parent'] as $val) {
                                ?>
                                <option value="<?=$val['id']?>" <?=!empty($model->industry_id)&&$model->industry_id==$val['id'] ? 'selected="selected"':''?>><?=$val['name']?></option>
                                <?php
                                if (!empty($category['child'][$val['id']])){
                                    foreach ($category['child'][$val['id']] as $value) {
                                        ?>
                                        <option value="<?=$value['id']?>"<?=!empty($model->industry_id)&&$model->industry_id==$value['id'] ? 'selected="selected"':''?>>┗━<?=$value['name']?></option>
                                        <?php
                                    }
                                }
                            }
                        }
                        ?>
                    </select>
                </div>
            </td>
            <td><?= $form->field($model, 'product')->textInput(['maxlength' => true]) ?></td>
        </tr>
        <tr>
            <td colspan="2">
                <?= $form->field($model, 'intro')->widget(\dosamigos\ckeditor\CKEditor::className(),[
                    'options' => ['row' => 10],
                    'preset' => 'basic',
                    'clientOptions' => [
                        'filebrowserUploadUrl' => \yii\helpers\Url::toRoute(['common/url', 'event' => 0]),
                    ]
                ]) ?>
            </td>
        </tr>
        <tr>
            <td><?= $form->field($model, 'contact_user')->textInput(['maxlength' => true]) ?></td>
            <td><?= $form->field($model, 'contact_mobile')->textInput(['maxlength' => true]) ?></td>
        </tr>
        <tr>
            <td><?= $form->field($model, 'contact_job')->textInput(['maxlength' => true]) ?></td>
            <td><?= $form->field($model, 'order')->textInput() ?></td>
        </tr>
    </table>

    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? Yii::t('common', 'create') : Yii::t('common', 'update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>
<script>
$(function () {
    //此处用了change事件，当选择好图片打开，关闭窗口时触发此事件
    $(".file_upload_attach").change(function (e) {
        var val = e.target.value;
        var point = val.lastIndexOf(".");
        var type = val.substr(point).toLowerCase();
        if (type != ".jpg" && type != ".jpeg" && type != ".png") {
            alert('系统目前只支持.jpg、.jpeg、.png文件格式');
            return false;
        }
        var file_id = $(this).attr('id');
        var data_id = $(this).data('id');
        $.ajaxFileUpload({
            //处理图片的脚本路径
            url: '<?=\yii\helpers\Url::toRoute(['common/upload-thumb', 'event' => 1, 'dir' => 'exhibitor'])?>',
            type: 'post',       //提交的方式
            secureuri: false,   //是否启用安全提交
            fileElementId: file_id,     //file控件ID
            dataType: 'json',  //服务器返回的数据类型
            success: function (data, status) {  //提交成功后自动执行的处理函数
                if (data.error) {
                    alert(data.msg);
                    return false;
                } else {
                    $("#"+data_id+"_input").val(data.fileUrl);
                }
            },
            error: function (data, status, e) {   //提交失败自动执行的处理函数
                alert(e);
            }
        })
    })

    $("#start_time").datetimepicker({
        format: "yyyy-mm-dd",
        //showMeridian: true,
        minView: "month",
        autoclose: true,
        todayBtn: true,
        language: 'zh'
    });
    $(".fa-remove").on('click', function(){
        $(this).parent().siblings("input").val('')
    })
})

getRegion(0, 'province');
function getRegion(obj, id){
    var pid = typeof obj == 'object' ? obj.value : obj;
    pid = typeof pid == 'undefined' || pid == '' ? 0 : pid;
    var html = '<option value=""><?=Yii::t('common', 'select...')?></option>';
    $("#"+id).html(html);
    $.ajax({
        url: "<?=Yii::$app->urlManager->createUrl(['/../common/get-region'])?>",
        data: {pid: pid},
        dataType: 'JSON',
        type: 'GET',
        success: function (response) {
            if (response.code == 0) {
                var data = response.data;
                if (data != '' && typeof data == 'object') {
                    for (var i in data) {
                        var selectd = false;
                        <?php
                        if (!empty($model->province_id)){
                        ?>
                        if (id == 'province' && i == parseInt('<?=$model->province_id?>')) {
                            selectd = true;
                        }
                        <?php
                        }
                        ?>
                        <?php
                        if (!empty($city_id)){
                        ?>
                        if (id == 'city' && i == parseInt('<?=$city_id?>')) {
                            selectd = true;
                        }
                        <?php
                        }
                        ?>
                        html += '<option value="' + i + '" ' + (selectd ? 'selected="selected"' : '') + '>' + data[i] + '</option>';
                    }
                }
            }
            $("#"+id).html(html);
        },
        error: function (error) {
            alert(JSON.stringify(error));
        }
    })
}
<?php
if (!empty($model->city_id)){
?>
getRegion(<?=$model->province_id?>, 'city');
<?php
}
?>
</script>
